package com.receive;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.utils.Constants;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * USB链接监听
 */
public class USBReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context content, Intent intent) {
        String action = intent.getAction();
        //0：断开、1：连上USB、2：连上了充电器
        if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
            int str= intent.getIntExtra("plugged",0);
            String regEx="[^0-9]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(String.valueOf(str));
            if("2".equals(m.replaceAll("").trim())){
                Constants.STATE_USB = true;
//                Toast.makeText(content,"手机已连接到电脑", Toast.LENGTH_SHORT).show();
            }else{
                Constants.STATE_USB = false;
                System.exit(-1);
//                Toast.makeText(content,"请将手机连接到电脑", Toast.LENGTH_SHORT).show();
            }
        }

    }
}